package com.tech.langk.testhttps;

import android.app.TimePickerDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.OkHttpClient;

public class MainActivity extends AppCompatActivity {

    EditText nameText;
    EditText passwordText;
    EditText numberText;
    Button chooseTime;
    Button push;
    ListView listView;
    TimeAdapter adapter;
    List<AutoTime> list = new ArrayList<>();

    LinearLayout stopLayout;
    Button stopText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameText = (EditText) findViewById(R.id.name);
        passwordText = (EditText) findViewById(R.id.password);
        numberText = (EditText) findViewById(R.id.number);
        chooseTime = (Button) findViewById(R.id.choose);
        push = (Button) findViewById(R.id.push);
        listView = (ListView) findViewById(R.id.list);
        stopLayout = (LinearLayout) findViewById(R.id.stoplayout);
        stopText = (Button) findViewById(R.id.stop);


        adapter = new TimeAdapter(list, MainActivity.this);
        listView.setAdapter(adapter);

        try {
            SignHttpsUtils.check(handler);
        } catch (Exception e) {
            e.printStackTrace();
            handler.sendEmptyMessage(-2);
        }

        chooseTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.clear();
                int number = Integer.parseInt(numberText.getText().toString());
                for (int i = 0; i < number; i++) {
                    list.add(new AutoTime());
                }
                adapter.notifyDataSetChanged();
            }
        });
        push.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JsonEntity jsonEntity = new JsonEntity();
                jsonEntity.setName(nameText.getText().toString());
                jsonEntity.setPassword(passwordText.getText().toString());
                jsonEntity.setList(list);
                String json = new Gson().toJson(jsonEntity);
                try {
                    SignHttpsUtils.run(handler, json);
                } catch (Exception e) {
                    handler.sendEmptyMessage(-1);
                    e.printStackTrace();
                }

            }
        });
        stopText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    SignHttpsUtils.stop(handler);
                } catch (Exception e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(-3);
                }
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case -1:
                    Toast.makeText(MainActivity.this, "发布失败了，联系男朋友吧", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    if (((boolean) msg.obj)) {
                        Toast.makeText(MainActivity.this, "发布成功了", Toast.LENGTH_SHORT).show();
                        stopLayout.setVisibility(View.VISIBLE);
                    } else {
                        Toast.makeText(MainActivity.this, "发布失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case -2:
                    stopLayout.setVisibility(View.VISIBLE);
                    stopText.setEnabled(false);
                    Toast.makeText(MainActivity.this, "检查是否发送中失败", Toast.LENGTH_SHORT).show();
                    stopText.setText("联系男朋友");
                    break;
                case 2:
                    if (((boolean) msg.obj)) {
                        Toast.makeText(MainActivity.this, "正在发送中", Toast.LENGTH_SHORT).show();
                        stopLayout.setVisibility(View.VISIBLE);
                    } else {
                        Toast.makeText(MainActivity.this, "没有发送", Toast.LENGTH_SHORT).show();
                        stopLayout.setVisibility(View.GONE);
                    }
                    break;
                case -3:
                    Toast.makeText(MainActivity.this, "停止失败，赶紧联系男朋友！", Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    if (((boolean) msg.obj)) {
                        Toast.makeText(MainActivity.this, "停止成功", Toast.LENGTH_SHORT).show();
                        stopLayout.setVisibility(View.GONE);
                    } else {
                        Toast.makeText(MainActivity.this, "停止失败，赶紧联系男朋友", Toast.LENGTH_SHORT).show();
                        stopLayout.setVisibility(View.VISIBLE);
                    }
                    break;
            }
        }
    };
}
